// 获取一些常用的公共数据
import { defineStore } from "pinia";
import { ElMessage } from 'element-plus';
import type { postPvPRequirementForm } from "@/api/common/type"
import { getItemList, getReqTypesList, getRoleUsers, getInitRequirement, postPVPRequirments } from "@/api/common";
import { getNewFatherRequirementStatus } from "@/api/newDemands/index";

let useCommonDataStore = defineStore('commonDataStore', {
    state: () => {
        return {
            // allRoleUsers: [],//! 所有的修复人员列表
            allProjectList: [] as any,//所有项目名称列表
            allReqTypesList: [] as any,//所有需求类型
            allRoleUserList: [] as any,//! 根据权限获取相应人员信息
            allInitRequirementData: [] as any,// 获取我负责的需求
            allPVPRequirementData: [] as any,// 获取我的研发任务
        }
    },
    actions: {
        // 获取所有项目名称列表allProjectList
        async getAllProjectList() {
            try {
                const {data} = await getItemList();

                if(data.code === 200) {
                    this.allProjectList = data.data.map((item: any) => {
                        return {
                            id: item.ID,
                            projectName: item.itemName
                        }
                    });
                    return this.allProjectList;
                } else {
                    ElMessage.error(data.msg);
                    throw console.error(data.msg);
                    
                }
            } catch (error) {
                throw error;
            }
        },
        // 获取所有需求类型allReqTypesList
        async getAllReqTypesList() {
            try {
                const { data } = await getReqTypesList();
                if (data.code === 200) {
                    this.allReqTypesList = data.data.map((item: any) => {
                        return {
                            id: item.ID,
                            type: item.requirementType,
                            bgColor: item.requirementTypeColor
                        }
                    });
                    return this.allReqTypesList;
                } else {
                    ElMessage.error(data.msg);
                    throw console.log(data.msg);
                }

            } catch (error) {
                throw error;
            }
        },
        // 根据权限获取相应人员信息
        async getAllRoleUsersList(index: any) {
            try {
                const { data } = await getRoleUsers(index);
                if (data.code === 200) {
                    const list = data.data.map((item: any) => {
                        return {
                            nickName: item.nickName,
                            headerImg: item.headerImg,
                            uuid: item.uuid,
                            authorityId: item.authorityId,
                            userWxId: item.userWxId,
                        }
                    });

                    this.allRoleUserList = list.filter((item: any) => item.userWxId !== 0).map((item:any) => item);

                    return this.allRoleUserList;
                } else {
                    ElMessage.error(data.msg);
                    throw console.log(data.msg);
                }
            } catch (error) {
                throw error;
            }
        },
        // 获取我负责的需求数据
        async getAllInitRequirement () {
            try {
                const { data } = await getInitRequirement();

                if (data.code === 200) {
                    this.allInitRequirementData = data.data;
                    return this.allInitRequirementData;
                } else {
                    ElMessage.error(data.msg);
                    throw console.error(data.msg);
                }
            } catch (error) {
                throw error;
            }
        },
        // 获取我的研发任务数据
        async postAllPVPRequirement (form: postPvPRequirementForm) {
            try {
                const { data } = await postPVPRequirments(form);

                if (data.code === 200) {
                    this.allPVPRequirementData = data.data;
                    return this.allPVPRequirementData;
                }
            } catch (error) {
                throw error;
            }
        },
        // 更新主需求状态fatherStatus
        async updateFatherStatus (id: number) {
            try {
                const { data } =  await getNewFatherRequirementStatus({
                    requirementId: id
                  });
                  
                  if(data.code === 200) {
                    console.log("更新fatherStatus成功");
                } else {
                      ElMessage.error(data.msg);
                      return;

                  }
            } catch (error) {
                throw error;
            }
        }

    }
})

export default useCommonDataStore;